package Server;

import javax.ws.rs.GET;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.Path;

import org.codehaus.jettison.json.JSONObject;

import Server.BusinessObjects.User;

@Path("/login")
public class LoginResource {

	@GET
	@Produces(MediaType.APPLICATION_JSON)
	@Path("/{username}/{password}")
	public String doLogin(@PathParam("username") String username,
			@PathParam("password") String password) {

		JSONObject o = new JSONObject();
		User u = null;

		try {
			u = User.authenticateUser(username, password);
			if (u != null) {
				o.put("UserId", u.getId());
				o.put("UserName", u.getName());
				o.put("IsAdmin", u.isAdmin());
			} else{
				o.put("Error", "Benutzername oder PW sind falsch");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return o.toString();
	}
}
